﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lamex.mIE.Calculate
{
    public class Cal_Seat_Value
    {
        //dt必须排序
        public void GetSeat_Value(DataTable dt)
        {
            string Part_No_Temp = "";
            double S02_CUT_LEATHER = 0, S02_CUT_FABRIC = 0, S02_SEW = 0, S02_CHECK_LEATHER = 0, S03_CUT_H = 0, S03_CUT_V = 0, S03_CUT_PUNCH = 0;
            double W = 0, L = 0;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                int DEMAND_QTY = Convert.ToInt32(dt.Rows[i]["DEMAND_QTY"].ToString().Length > 0 ? dt.Rows[i]["DEMAND_QTY"].ToString() : "0");
                int FRT_SIZ = Convert.ToInt32(dt.Rows[i]["FRT_SIZ"].ToString().Length > 0 ? dt.Rows[i]["FRT_SIZ"].ToString() : "0");
                int FRT_QTY = Convert.ToInt32(dt.Rows[i]["FRT_QTY"].ToString().Length > 0 ? dt.Rows[i]["FRT_QTY"].ToString() : "0");
                int BCK_SIZ = Convert.ToInt32(dt.Rows[i]["BCK_SIZ"].ToString().Length > 0 ? dt.Rows[i]["BCK_SIZ"].ToString() : "0");
                int BCK_QTY = Convert.ToInt32(dt.Rows[i]["BCK_QTY"].ToString().Length > 0 ? dt.Rows[i]["BCK_QTY"].ToString() : "0");
                int LFT_SIZ = Convert.ToInt32(dt.Rows[i]["LFT_SIZ"].ToString().Length > 0 ? dt.Rows[i]["LFT_SIZ"].ToString() : "0");
                int LFT_QTY = Convert.ToInt32(dt.Rows[i]["LFT_QTY"].ToString().Length > 0 ? dt.Rows[i]["LFT_QTY"].ToString() : "0");
                int RGT_SIZ = Convert.ToInt32(dt.Rows[i]["RGT_SIZ"].ToString().Length > 0 ? dt.Rows[i]["RGT_SIZ"].ToString() : "0");
                int RGT_QTY = Convert.ToInt32(dt.Rows[i]["RGT_QTY"].ToString().Length > 0 ? dt.Rows[i]["RGT_QTY"].ToString() : "0");
                double SPONGE_LEN = Convert.ToDouble(dt.Rows[i]["SPONGE_LEN"].ToString().Length > 0 ? dt.Rows[i]["SPONGE_LEN"].ToString() : "0");
                double SPONGE_WID = Convert.ToDouble(dt.Rows[i]["SPONGE_WID"].ToString().Length > 0 ? dt.Rows[i]["SPONGE_WID"].ToString() : "0");
                double SPONGE_THI = Convert.ToDouble(dt.Rows[i]["SPONGE_THI"].ToString().Length > 0 ? dt.Rows[i]["SPONGE_THI"].ToString() : "0");

                //S02_裁布真皮
                if (dt.Rows[i]["MATERIAL_TYPE"].ToString() == "真皮")
                    S02_CUT_LEATHER = (FRT_SIZ + BCK_SIZ + LFT_SIZ + RGT_SIZ) * DEMAND_QTY;
                //S02_裁布普通
                if (dt.Rows[i]["MATERIAL_TYPE"].ToString() == "布(含假皮及网布)")
                    S02_CUT_FABRIC = (FRT_SIZ + BCK_SIZ + LFT_SIZ + RGT_SIZ) * DEMAND_QTY;
                //S02_电车车缝
                if (dt.Rows[i]["MATERIAL_TYPE"].ToString() != "绵")
                    S02_SEW = (FRT_SIZ * FRT_QTY + BCK_SIZ * BCK_QTY + LFT_SIZ * LFT_QTY + RGT_SIZ * RGT_QTY) * DEMAND_QTY;
                //S02_真皮检查
                if (dt.Rows[i]["MATERIAL_TYPE"].ToString() == "真皮")
                    S02_CHECK_LEATHER = Maximum(FRT_SIZ * LFT_SIZ, FRT_SIZ * RGT_SIZ, BCK_SIZ * LFT_SIZ, BCK_SIZ * RGT_SIZ) * DEMAND_QTY;
                //S03_平刀机
                if (dt.Rows[i]["MATERIAL_TYPE"].ToString() == "绵" &&
                    dt.Rows[i]["MACHINING_TYPE"].ToString().Contains("平刀"))
                {
                    if (dt.Rows[i]["MATERIAL_SIZE"].ToString().Length > 0)
                    {
                        string[] W_L = dt.Rows[i]["MATERIAL_SIZE"].ToString().Split('*');
                        W = Convert.ToDouble(W_L[0]);
                        L = Convert.ToDouble(W_L[1]);
                    }
                    S03_CUT_H = 1 / (Math.Max(Math.Floor(W / (SPONGE_LEN + 5)) * Math.Floor(L / (SPONGE_WID + 5)),
                        Math.Floor(L / (SPONGE_LEN + 5)) * Math.Floor(W / (SPONGE_WID + 5)))) * DEMAND_QTY;
                }
                //S03_直刀机
                if (dt.Rows[i]["MATERIAL_TYPE"].ToString() == "绵" &&
                 dt.Rows[i]["MACHINING_TYPE"].ToString().Contains("直刀"))
                {
                    S03_CUT_V = (SPONGE_LEN + SPONGE_WID * 2) * DEMAND_QTY;
                }
                //S03_冲床
                if (dt.Rows[i]["MATERIAL_TYPE"].ToString() == "绵" &&
                dt.Rows[i]["MACHINING_TYPE"].ToString().Contains("冲床"))
                {
                    S03_CUT_PUNCH = DEMAND_QTY;
                }

                if (dt.Rows[i]["PART_NO"].ToString() != Part_No_Temp)
                {

                }
                else
                {

                }
                Part_No_Temp = dt.Rows[i]["PART_NO"].ToString();
            }
        }

        //二个数取最大值
        private static double Maximun2(double Tw1, double Tw2)
        {
            if (Tw1 > Tw2)
            {
                return Tw1;
            }
            return Tw2;
        }
        //四个数取最大值
        public double Maximum(double x, double y, double z, double h)
        {
            double maximumValue = x;
            if (y > maximumValue)
                maximumValue = y;
            if (z > maximumValue)
                maximumValue = z;
            if (h > maximumValue)
                maximumValue = h;
            return maximumValue;
        }
        
    }

}
